package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.pk.entity.SsPkYzddMyRank;
import net.huashitong.ssydt.provider.pk.mapper.SsPkYzddMyRankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.18
 */
@Service
@Transactional
public class SsPkYzddMyRankServiceImpl extends AbstractLongService<SsPkYzddMyRank> implements SsPkYzddMyRankService {

    private SsPkYzddMyRankMapper mapper;

    private SsPkYzddMatchRecordService ssPkYzddMatchRecordService;

    @Autowired
    public SsPkYzddMyRankServiceImpl(SsPkYzddMyRankMapper mapper,
                                     SsPkYzddMatchRecordService ssPkYzddMatchRecordService) {
        this.mapper = mapper;
        this.ssPkYzddMatchRecordService = ssPkYzddMatchRecordService;
    }

    @Override
    protected LongMapper<SsPkYzddMyRank> getLongMapper() {
        return this.mapper;
    }

    @Override
    public SsPkYzddMyRank getAutoUpdateRankByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYzddMyRank ssPkYzddMyRank = this.getAutoLoadLastRankByMatchRecordIdWithUserId(matchRecordId, userId);
        this.updateRank(ssPkYzddMyRank);
        return ssPkYzddMyRank;
    }

    @Override
    public SsPkYzddMyRank getAutoLoadLastRankByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYzddMyRank ssPkYzddMyRank = this.getByMatchRecordIdWithUserId(matchRecordId, userId);
        if (ssPkYzddMyRank == null) {
            int lastRanking = 0;
            BigDecimal lastHighestScore = BigDecimal.ZERO;
            Long lastMatchRecordId = ssPkYzddMatchRecordService.getLastMatchRecordId(matchRecordId);
            if (lastMatchRecordId != null && lastMatchRecordId != 0L) {
                SsPkYzddMyRank lastObj = this.getByMatchRecordIdWithUserId(lastMatchRecordId, userId);
                if (lastObj != null) {
                    lastObj = this.updateRank(lastObj);
                    lastRanking = lastObj.getRanking();
                    lastHighestScore = lastObj.getHighestScore();
                }
            }

            ssPkYzddMyRank = new SsPkYzddMyRank();
            ssPkYzddMyRank.setMatchRecordId(matchRecordId);
            ssPkYzddMyRank.setUserId(userId);
            ssPkYzddMyRank.setRanking(0);
            ssPkYzddMyRank.setHighestScore(BigDecimal.ZERO);
            ssPkYzddMyRank.setHighestGmtModified(new Date(System.currentTimeMillis()));
            ssPkYzddMyRank.setLastRanking(lastRanking);
            ssPkYzddMyRank.setLastHighestScore(lastHighestScore);
            this.mapper.insertSelective(ssPkYzddMyRank);
        }
        return ssPkYzddMyRank;
    }

    @Override
    public SsPkYzddMyRank updateRank(SsPkYzddMyRank ssPkYzddMyRank) {
        int ranking = this.mapper.getRankByHighestScoreDescAndHighestGmtModifiedAsc(ssPkYzddMyRank);
        if (ranking > 1000) {
            ranking = 0;
        }
        ssPkYzddMyRank.setRanking(ranking);
        this.updateByPrimaryKey(ssPkYzddMyRank);
        return ssPkYzddMyRank;
    }

    @Override
    public SsPkYzddMyRank getByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYzddMyRank ssPkYzddMyRank = new SsPkYzddMyRank();
        ssPkYzddMyRank.setMatchRecordId(matchRecordId);
        ssPkYzddMyRank.setUserId(userId);
        return this.mapper.getByMatchRecordIdWithUserId(ssPkYzddMyRank);
    }

    @Override
    public List<SsPkYzddMyRank> findHighestScoreDescByMatchRecordId(Long matchRecordId) {
        return this.mapper.findHighestScoreDescByMatchRecordId(matchRecordId);
    }
}
